package com.tomdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomdd.mapper.CategoryMapper;
import com.tomdd.model.entity.Category;
import com.tomdd.model.req.CategoryReq;
import com.tomdd.service.CategoryService;
import com.tomdd.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author zhongxu
* @description 针对表【category】的数据库操作Service实现
* @createDate 2024-07-17 21:27:40
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Override
    public void add(CategoryReq category) {
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);
        baseMapper.add(category);
    }

    @Override
    public List<Category> listByUserId() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return listByIds(Collections.singletonList(userId));
    }

    @Override
    public Category findById(Integer id) {
        return baseMapper.findById(id);
    }

    @Override
    public void updateCategory(CategoryReq categoryReq) {
        Category category = BeanUtil.copyProperties(categoryReq, Category.class);
        category.setUpdateTime(new Date());
        baseMapper.updateCategory(category);
    }
}




